<script>
$(document).ready(function() {
	/*
	 * Start page functions
	 * 
	 */	
	function updateSelectOptionList(){
		//alert('Updating information');
		$.ajax({
			url: "/ws/getRoomData",
			//dataType: 'json',
			success: function(jsonData){
				//alert("Got this " + jsonData)
				//var options = '<option value="NONE"> - </option>';
				var options = '';
				var jd = $.parseJSON(jsonData)
				for (var i = 0; i < jd.length; i++) {
					options += '<option value="' + jd[i].rid + '">' + jd[i].rname + ' (' + jd[i].rsize + ')' + '</option>';
				}
				//alert("here are options " + options)
				$("select#selRoomId").html(options);
			}
		});
		$.ajax({
			url: "/ws/getUserData",
			//dataType: 'json',
			success: function(jsonData){
				//alert("Got this " + jsonData)
				var options = '';
				var jd = $.parseJSON(jsonData)
				for (var i = 0; i < jd.length; i++) {
					options += '<option value="' + jd[i].uid + '">' + jd[i].namelast + ', ' + jd[i].namefirst + '</option>';
				}
				//alert("here are options " + options)
				$("select#selUserId").html(options);
			}
		});
		
	}
	//updateSelectOptionList();
	/*
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	// Select control
	$("#adminChoice").change(function(){
		$(".adminTool").hide();
		var choice = $("#adminChoice").val()
		//alert("Choice: " + choice);
		var choiceDiv = choice +"Div";
		$("#"+choiceDiv).fadeIn(1300);
	});
	/*
	 * 
	 */
	// User ADD
	$("input[id=addUserBtn]").click(function() {
		//alert("Click");
		if ($("#userAddID").validate().form()) {
			$.ajax({
				url: "/admin/userAddWs",
				async: false,
				data: $("#userAddID :input").serialize(),
				success: function(response) {
					if (response == 'Success') {
						$("#formResult").html("<div id='resultDiv' class='success'>User added Successfully</div> ");
						updateSelectOptionList();
					} else { 
						$("#formResult").html("<div id='resultDiv' class='fail'>Failed to add user to system</div> ");
					}
				}
			});
			$("#resultDiv").fadeOut(6000);
		}
	});
	
	$("#userEditID").validate();
	$("#roomEditID").validate();
	
	// User Remove
	$("input[id=delUserBtn]").click(function() {
		//alert("Clicked");
		
		$.ajax({
			url: "/admin/userDelWs",
			//dataType: 'json',
			async: false,
			data: $("#userDelID :input").serialize(),
			success: function(response) {
				
				//alert("Response: " + response);
				if (response == 'Success') {
					$("#formResult").html("<div id='resultDiv' class='success'>User removed from system</div> ");
					updateSelectOptionList();
					
				
				} else { 
					$("#formResult").html("<div id='resultDiv' class='fail'>Failed to remove user from system</div> ");
				}
			}
		});
		$("#resultDiv").fadeOut(6000);
	});
	

	
	$("input[id=addRoomBtn]").click(function() {
		//alert("Click");
		if ($("#roomAddID").validate().form()) {
			$.ajax({
				url: "/admin/roomAddWs",
				async: false,
				data: $("#roomAddID :input").serialize(),
				success: function(response) {
					if (response == 'Success') {
						$("#formResult").html("<div id='resultDiv' class='success'>Room Added to System</div> ");
						updateSelectOptionList();
					} else { 
						$("#formResult").html("<div id='resultDiv' class='fail'>Failed to add Room to the system</div> ");
					}
				}
			});
			$("#resultDiv").fadeOut(6000);
		}
	});
	
	
	
	$("input[id=modRoomBtn]").click(function() {
		//alert("Click");
		if ($("#roomEditID").validate().form()) {
			$.ajax({
				url: "/admin/roomModWs",
				async: false,
				data: $("#roomEditID :input").serialize(),
				success: function(response) {
					if (response == 'Success') {
						$("#formResult").html("<div id='resultDiv' class='success'>Room information updated in System</div> ");
						updateSelectOptionList();
					} else { 
						$("#formResult").html("<div id='resultDiv' class='fail'>Failed to update Room to the system</div> ");
					}
				}
			});
			$("#resultDiv").fadeOut(6000);
		}
	});
	
	$("input[id=modUserBtn]").click(function() {
		//alert("Click");
		if ($("#userEditID").validate().form()) {
			$.ajax({
				url: "/admin/userModWs",
				async: false,
				data: $("#userEditID :input").serialize(),
				success: function(response) {
					if (response == 'Success') {
						$("#formResult").html("<div id='resultDiv' class='success'>User information updated in  System</div> ");
						updateSelectOptionList();
					} else { 
						$("#formResult").html("<div id='resultDiv' class='fail'>Failed to update User to the system</div> ");
					}
				}
			});
			$("#resultDiv").fadeOut(6000);
		}
	});
	
	
	
	
	$(".editUser").change(function(){
		var value = $(".editUser").val();
		//alert("selected -> " + value);
		// get user data and populate form 
		$.ajax({
			url: "/ws/getUserNameById",
			dataType: 'json',
			async: false,
			data: {'uid': value, 'format': 'json'},
			success: function(response) {
				$("#editName").val(response['uname']);
				$("#editFirst").val(response['namefirst']);
				$("#editLast").val(response['namelast']);
				$("#editEmail").val(response['email']);
				$("#editUID").val(response['uid']);
				
				
				if (response['isadmin'] == 1) {
					
					$('#editAdmin').attr('checked','checked')
				} else {
					$('#editAdmin').removeAttr('checked')
				}
				

			}
		});
	});
	
	
	

	$(".editRoom").change(function(){
		var value = $(".editRoom").val();
		//alert("selected -> " + value);
		// get user data and populate form 
		$.ajax({
			url: "/ws/getRoomById",
			dataType: 'json',
			async: false,
			data: {'rid': value},
			success: function(response) {
				//alert("Got -> " +response['rname'])
				$("#editRName").val(response['rname']);
				$("#editRDesc").val(response['rdescription']);
				$("#editRSize").val(response['rsize']);
				$("#editRID").val(response['rid']);
				
			}
		});
	});
	
	
	
	
	/*
	$("input[id=roomUserBtn]").click(function() {
		//alert("Clicked");
		
		$.ajax({
			url: "/admin/roomDelWs",

			async: false,
			data: $("#roomDelID :input").serialize(),
			success: function(response) {
				
				//alert("Response: " + response);
				if (response == 'Success') {
					$("#formResult").html("<div id='resultDiv' class='success'>User removed from system</div> ");
					updateSelectOptionList();
					
				
				} else { 
					$("#formResult").html("<div id='resultDiv' class='fail'>Failed to remove user from system</div> ");
				}
			}
		});
		$("#resultDiv").fadeOut(6000);
	});
	*/
	
});	
</script>
